<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Login extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('m_account');
    }

    function index() {
       redirect('Home');
    }

    function cek_login() {
        $this->load->library('form_validation');
        $this->form_validation->set_rules('frm_username', 'Username', 'required|alpha_dash|min_length[4]');
        $this->form_validation->set_rules('frm_password', 'Password', 'required|min_length[6]');
        $data['username'] = $this->input->post('frm_username');
        $data['password'] = md5($this->input->post('frm_password'));

        if ($this->form_validation->run() != FALSE) {
            $result = $this->m_account->select_a_record($data);
            if ($result != false) {
                $this->m_account->get_cookie();
                redirect(current_url());
            } else {
                redirect('Login/login_failed');
            }
        } else {
            redirect('Login/login_failed');
        }
    }
    
    function login_failed() {
        if(isset($_COOKIE['c_iduser'])){
            redirect(base_url());
        }
        $data['title'] = "Travelina";
        $data['page'] = "home";
        $this->load->model('m_location');
        $data['location'] = $this->m_location->get_all_location();
        $data['message'] = "Username or Password is incorrect";
        $this->load->view('home', $data);
    }

    function logout() {
        
        $this->m_account->delete_cookie();
        redirect(base_url());
    }

    function cek_login_admin() {
        
    }

}
